<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.4/css/all.min.css" integrity="sha256-mUZM63G8m73Mcidfrv5E+Y61y7a12O5mW4ezU3bxqW4=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"tallgy.gitee.io","root":"/","images":"/images","scheme":"Muse","darkmode":false,"version":"8.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}}</script><script src="/js/config.js"></script>
<meta name="description" content="浏览器的回流和重绘1https:&#x2F;&#x2F;juejin.cn&#x2F;post&#x2F;6844903753783443463  先放一张图 ![painted and re-render](浏览器的回流和重绘&#x2F;painted and re-render.jpg) 浏览器解析DOM树的过程  DOM 树其实就是，浏览器用来描述节点信息的数据类型。  当浏览器获取到html文件时，开始解析和构造DOM树，我们也知道htm">
<meta property="og:type" content="article">
<meta property="og:title" content="浏览器的回流和重绘">
<meta property="og:url" content="http://tallgy.gitee.io/2021/11/17/%E6%B5%8F%E8%A7%88%E5%99%A8/%E6%B5%8F%E8%A7%88%E5%99%A8%E7%9A%84%E5%9B%9E%E6%B5%81%E5%92%8C%E9%87%8D%E7%BB%98/index.html">
<meta property="og:site_name" content="tallgy&#39;s blog">
<meta property="og:description" content="浏览器的回流和重绘1https:&#x2F;&#x2F;juejin.cn&#x2F;post&#x2F;6844903753783443463  先放一张图 ![painted and re-render](浏览器的回流和重绘&#x2F;painted and re-render.jpg) 浏览器解析DOM树的过程  DOM 树其实就是，浏览器用来描述节点信息的数据类型。  当浏览器获取到html文件时，开始解析和构造DOM树，我们也知道htm">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-11-17T02:11:43.000Z">
<meta property="article:modified_time" content="2023-06-25T08:57:29.354Z">
<meta property="article:author" content="tallgy">
<meta property="article:tag" content="随笔">
<meta property="article:tag" content="回流和重绘">
<meta property="article:tag" content="性能">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://tallgy.gitee.io/2021/11/17/%E6%B5%8F%E8%A7%88%E5%99%A8/%E6%B5%8F%E8%A7%88%E5%99%A8%E7%9A%84%E5%9B%9E%E6%B5%81%E5%92%8C%E9%87%8D%E7%BB%98/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://tallgy.gitee.io/2021/11/17/%E6%B5%8F%E8%A7%88%E5%99%A8/%E6%B5%8F%E8%A7%88%E5%99%A8%E7%9A%84%E5%9B%9E%E6%B5%81%E5%92%8C%E9%87%8D%E7%BB%98/","path":"2021/11/17/浏览器/浏览器的回流和重绘/","title":"浏览器的回流和重绘"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>浏览器的回流和重绘 | tallgy's blog</title>
  




  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">tallgy's blog</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>







</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E6%B5%8F%E8%A7%88%E5%99%A8%E7%9A%84%E5%9B%9E%E6%B5%81%E5%92%8C%E9%87%8D%E7%BB%98"><span class="nav-number">1.</span> <span class="nav-text">浏览器的回流和重绘</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%B5%8F%E8%A7%88%E5%99%A8%E8%A7%A3%E6%9E%90DOM%E6%A0%91%E7%9A%84%E8%BF%87%E7%A8%8B"><span class="nav-number">1.1.</span> <span class="nav-text">浏览器解析DOM树的过程</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%87%8D%E7%BB%98"><span class="nav-number">1.2.</span> <span class="nav-text">重绘</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%87%8D%E6%8E%92-%E5%9B%9E%E6%B5%81"><span class="nav-number">1.3.</span> <span class="nav-text">重排 &#x2F; 回流</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%A6%82%E4%BD%95%E5%87%8F%E5%B0%91%E9%87%8D%E6%8E%92"><span class="nav-number">1.4.</span> <span class="nav-text">如何减少重排</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#transform%E5%B1%9E%E6%80%A7%E4%B8%BA%E4%BB%80%E4%B9%88%E4%B8%8D%E4%BC%9A%E5%BC%95%E8%B5%B7%E9%87%8D%E6%8E%92"><span class="nav-number">1.5.</span> <span class="nav-text">transform属性为什么不会引起重排</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">tallgy</p>
  <div class="site-description" itemprop="description">只是一个知识的搬运工</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">107</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">32</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">74</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://tallgy.gitee.io/2021/11/17/%E6%B5%8F%E8%A7%88%E5%99%A8/%E6%B5%8F%E8%A7%88%E5%99%A8%E7%9A%84%E5%9B%9E%E6%B5%81%E5%92%8C%E9%87%8D%E7%BB%98/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="tallgy">
      <meta itemprop="description" content="只是一个知识的搬运工">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="tallgy's blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          浏览器的回流和重绘
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-11-17 10:11:43" itemprop="dateCreated datePublished" datetime="2021-11-17T10:11:43+08:00">2021-11-17</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2023-06-25 16:57:29" itemprop="dateModified" datetime="2023-06-25T16:57:29+08:00">2023-06-25</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E9%9A%8F%E7%AC%94/" itemprop="url" rel="index"><span itemprop="name">随笔</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="浏览器的回流和重绘"><a href="#浏览器的回流和重绘" class="headerlink" title="浏览器的回流和重绘"></a>浏览器的回流和重绘</h1><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">https://juejin.cn/post/6844903753783443463</span><br></pre></td></tr></table></figure>

<p>先放一张图</p>
<p>![painted and re-render](浏览器的回流和重绘/painted and re-render.jpg)</p>
<h2 id="浏览器解析DOM树的过程"><a href="#浏览器解析DOM树的过程" class="headerlink" title="浏览器解析DOM树的过程"></a>浏览器解析DOM树的过程</h2><p>  DOM 树其实就是，浏览器用来描述节点信息的数据类型。</p>
<ul>
<li><p>当浏览器获取到html文件时，开始解析和构造DOM树，我们也知道html文件里面有 JavaScript、 CSS 和 其他文件</p>
<ul>
<li><p>遇到了 JavaScript，代码或者文件，在默认情况下，会停止DOM树的执行。因为 JavaScript 可以操作 DOM 。当然也有 defer 和 async 两个模式 </p>
<ul>
<li><pre><code>&lt;script defer async&gt;&lt;/script&gt;

当然这里 defer 和 async 的区别我们后续在讲解。
</code></pre>
</li>
<li><p>但是如果JavaScript操作了CSS，那么就会转而构造CSSOM树。</p>
</li>
</ul>
</li>
<li><p>遇到了CSS 和 其他文件，会先进行下载，CSS 会在DOM树的构造完成之后再进行构造。</p>
</li>
</ul>
</li>
<li><p>此时，html已经解析完成，DOM树 和 CSSOM 树也已经构造完成，我们就需要将DOM树和CSSOM树进行渲染为 render tree 或者叫做 layout tree，设置了display:none 的不会出现在渲染树上，但是对于before伪元素，就会出现在渲染树上。</p>
</li>
<li><p>生成了渲染树之后，便是进行绘制painting了。</p>
</li>
<li><p>painting之后便可以进行显示</p>
</li>
</ul>
<h2 id="重绘"><a href="#重绘" class="headerlink" title="重绘"></a>重绘</h2><p>我们可以知道页面的显示是要先进行渲染树的生成和painting，</p>
<p>而painting就是绘制，重绘就是指渲染树不会重新生成，但是需要重新绘制的情况。</p>
<p>常见的重绘就是颜色的变化，因为他不会引起布局的改变。</p>
<h2 id="重排-回流"><a href="#重排-回流" class="headerlink" title="重排 / 回流"></a>重排 / 回流</h2><p>重排就是渲染树重新进行了改变，一般来说就是导致了布局的变化就会导致重排。</p>
<p>重排会引起重绘。但是重绘不一定会引起重排</p>
<p>现在的浏览器也对频繁的重排重绘进行过优化，比如会有维护重排和重绘的队列，只有在达到阈值或者一定时间之后才会进行统一的调用，这样可以减少变化。</p>
<p>当然如果你使用了 clientWidth 这种属性，这一系列属性可以获取到标签的位置信息等的，浏览器会为了拿到一个最精准的属性会强制清空队列。</p>
<h2 id="如何减少重排"><a href="#如何减少重排" class="headerlink" title="如何减少重排"></a>如何减少重排</h2><p>减少的形式有很多：</p>
<ul>
<li>减少对渲染树的操作，比如可以将多次的操作合并为一个：<ul>
<li>使用CSS的类进行操作。</li>
<li>使用display:none 进行操作，因为这样在不显示的情况下是不会引起重排的，然后再操作完之后再显示，那么就只会造成两次重排。</li>
<li>使用 documentFragment，将操作放入这里，然后最后再添加到文档中</li>
</ul>
</li>
<li>减少使用属性，利用缓存，减少对属性的请求</li>
<li>尽量从末端修改元素</li>
<li>可以使用 绝对定位和固定定位，使其脱离文档流</li>
<li>使用transform属性代替其他操作。</li>
</ul>
<h2 id="transform属性为什么不会引起重排"><a href="#transform属性为什么不会引起重排" class="headerlink" title="transform属性为什么不会引起重排"></a>transform属性为什么不会引起重排</h2><p>这里简单来说，就是说浏览器在进行渲染的时候，是将其分成了多个图层，使用的复合图层的形式进行的操作。</p>
<p>当我们调用 transform的时候，会生成一个渲染层，对于这个渲染层会有独立的合成层。因此对其进行修改并不会影响到页面整体的渲染树的重新生成。</p>
<p>就是说，浏览器是将页面分成了不同的图层，然后根据不同的图层进行了划分。然后在显示的时候再根据z-index这种类型的进行一个高低的合成。</p>
<p><strong>独立的合成层如何产生</strong>：</p>
<ul>
<li>使用了 3D 或者 perspective transform</li>
<li>video</li>
<li>canvas</li>
<li>flash</li>
<li>opacity</li>
<li>CSS滤镜 filters</li>
<li>存在合成层后代的</li>
<li>和合成层重叠且在该层上面渲染的。</li>
</ul>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/%E9%9A%8F%E7%AC%94/" rel="tag"># 随笔</a>
              <a href="/tags/%E5%9B%9E%E6%B5%81%E5%92%8C%E9%87%8D%E7%BB%98/" rel="tag"># 回流和重绘</a>
              <a href="/tags/%E6%80%A7%E8%83%BD/" rel="tag"># 性能</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2021/11/16/Vue/Vue%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F/" rel="prev" title="Vue生命周期">
                  <i class="fa fa-chevron-left"></i> Vue生命周期
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2021/11/19/%E9%9A%8F%E7%AC%94/nginx%E7%9A%84%E4%BD%BF%E7%94%A8/" rel="next" title="nginx的使用">
                  nginx的使用 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">tallgy</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  





  





</body>
</html>
